<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class CaseReport extends Model
{
    protected $table = "c_case_reports";
    
    /****
     * @param $data
     * @return bool
     * 添加病历夹
     */
    public function doSetting($data)
    {
        if (isset($data['id']) && !empty($data['id'])) {
            $redata = $this->getInfo($data['id']);
            $redata->case_id = $data['case_id'];
            $redata->images = $data['images'];
            $redata->ins_times = $data['ins_times'];
            $redata->user_id = $data['user_id'];
            $redata->tags = isset($data['tags'])?$data['tags']:'';
            $res = $redata->save();
        } else {
            //添加
            $this->case_id = $data['case_id'];
            $this->images = $data['images'];
            $this->ins_times = $data['ins_times'];
            $this->user_id = $data['user_id'];
            $this->tags = isset($data['tags'])?$data['tags']:'';
            $res = $this->save();
        }
        return $res;
    }


    /***
     * @param $data
     * @return mixed
     * 获取病历夹信息
     */
    public function getInfo($id)
    {
        return $this->where([['id', '=', $id]])->first();
    }

    /****
     * @param $data
     * 获取病历夹列表
     */
    public function getCaseReportList($param)
    {
        $where = [];
        if (isset($param['start']) && !empty($param['start'])) {  //开始时间
            $where[] = ['ins_times', '>=', $param['start']];
        }
        if (isset($param['end']) && !empty($param['end'])) {  //开始时间
            $where[] = ['ins_times', '<=', $param['end']];
        }
        if (isset($param['title']) && !empty($param['title'])) {  //标题
            $where[] = ['tags', 'like', '%' . $param['title'] . '%'];
        }
        $list = $this->where([['case_id', '=', $param['case_id']]])->where($where)->orderByDesc("ins_times")->get();
        if (!empty($list)) {
            $list = $list->toArray();
            $list = self::array_group_by($list, 'ins_times');
            return $list;
        }
        return [];
    }
    /****
     * @param $data
     * 获取病历夹列表Android使用
     */
    public function getCaseReportListAndroid($param)
    {
        $where = [];
        if (isset($param['start']) && !empty($param['start'])) {  //开始时间
            $where[] = ['ins_times', '>=', $param['start']];
        }
        if (isset($param['end']) && !empty($param['end'])) {  //开始时间
            $where[] = ['ins_times', '<=', $param['end']];
        }
        if (isset($param['title']) && !empty($param['title'])) {  //标题
            $where[] = ['tags', 'like', '%' . $param['title'] . '%'];
        }
        $list = $this->where([['case_id', '=', $param['case_id']]])->where($where)->orderByDesc("ins_times")->get();
        if (!empty($list)) {
            $list = $list->toArray();
            $url = Url::getHttpHost();
            foreach($list as $k=>&$v){
                if(!empty($v['images'])){
                    $v['images'] = $url.'upload/'.$v['images'];
                }
            }
//            $list = self::array_group_by($list, 'ins_times');
            return $list;
        }
        return [];
    }




    /***
     * @param $list
     * @return array
     * 分组排序
     */
    public static function array_group_by($list)
    {
        $newArray = array();
        $host = new Url();
        foreach ($list as $v) {
            $v['images'] = $host::getHttpHost().'upload/'.$v['images'];
            $newArray[$v['ins_times']][] = $v;
        }
        return $newArray;
    }

    public function delCaseReport($data)
    {
        return $this::destroy($data['id']);
    }
}
